/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import xmlresources.Memory;
import xmlresources.Cpuu;
import xmlresources.Process;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import server.Cpu;
import server.Memoire;
import server.Monitor;
import server.Processus;
import donnees.Bdd;

/**
 *
 * @author juge-e
 */
@XmlRootElement
public class MonitorClient {

    private Monitor monMonitor;
    private String ip;
    private Memoire mem = new Memoire();
    private Processus proc = new Processus();
    private Cpu cpu = new Cpu();
    private Bdd maBdd = new Bdd();

    /**
     * Initialise le client avec un nom de serveur
     *
     * @param NomServeur
     */
    public void initialiser(String nomServeur) throws NotBoundException, MalformedURLException, RemoteException {
        monMonitor = (Monitor) Naming.lookup("//" + nomServeur + "/Monitor");
        ip=nomServeur;
    }

    public Memoire getMemory() throws RemoteException {
        if (null != monMonitor) {
            mem = monMonitor.getMemory(ip);
        }
        maBdd.addMemoryBDD(ip, mem.getFree(), mem.getUsed(), mem.getTotal());
        return mem;
    }
    
    @XmlElement
    public Memory getMemoire() throws RemoteException {
        if (null != monMonitor) {
            mem = monMonitor.getMemory(ip);
        }
        Memory temp = new Memory();
        temp.setLibre(mem.getFree());
        temp.setUtilisee(mem.getUsed());
        temp.setTotale(mem.getTotal());
        
        maBdd.addMemoryBDD(ip, mem.getFree(), mem.getUsed(), mem.getTotal());
        
        return temp;
    }


    @XmlElement
    public Cpuu getMonCpu() throws RemoteException {
        Cpuu cpuu = new Cpuu();
        if (null != monMonitor) {
            cpu = monMonitor.getCpu(ip);
        }
        cpuu.setCpu(cpu.getCpu());
       
        maBdd.addCpuBDD(ip, cpu.getCpu());
        return cpuu;

    }

    public Cpu getCpu() throws RemoteException {
        if (null != monMonitor) {
            cpu = monMonitor.getCpu(ip);
        }
        maBdd.addCpuBDD(ip, cpu.getCpu());
        return cpu;

    }

    @XmlElement
    public Process getMesProcessus() throws RemoteException {
        Process retour = new Process();
        if (null != monMonitor) {
            proc = monMonitor.getProcessus(ip);
        }
        retour.setProcess(proc.getProcess());
        
        maBdd.addTaskBDD(ip, proc.getProcess());
        
        return retour;
    }

    public Processus getProcessus() throws RemoteException {
        if (null != monMonitor) {
            proc = monMonitor.getProcessus(ip);
        }
        maBdd.addTaskBDD(ip, proc.getProcess());
        return proc;
    }

}
